import { payApi } from '@/api/pay'

export function usePayment() {
    // 初始化微信配置
    const initWxConfig = async () => {
        try {
            const res = await payApi.getWxConfig(window.location.href)
            wx.config({
                debug: false,
                appId: res.appId,
                timestamp: res.timestamp,
                nonceStr: res.nonceStr,
                signature: res.signature,
                jsApiList: ['chooseWXPay']
            })

            return new Promise((resolve, reject) => {
                wx.ready(() => resolve())
                wx.error((err) => reject(err))
            })
        } catch (error) {
            console.error('初始化微信配置失败:', error)
            throw error
        }
    }

    // 创建订单
    const createOrder = async (orderData) => {
        try {
            const res = await payApi.createOrder(orderData)
            return res.data
        } catch (error) {
            console.error('创建订单失败:', error)
            throw error
        }
    }

    // 检查订单状态
    const checkOrder = async (orderNo) => {
        try {
            const res = await payApi.checkOrderStatus(orderNo)
            return res.data
        } catch (error) {
            console.error('检查订单状态失败:', error)
            throw error
        }
    }

    return {
        initWxConfig,
        createOrder,
        checkOrder
    }
} 